<html>
<head><meta charset="utf-8"><title>Getting code representation of expression from HIR · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20code.20representation.20of.20expression.20from.20HIR.html">Getting code representation of expression from HIR</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="225927435"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20code%20representation%20of%20expression%20from%20HIR/near/225927435" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boolean_coercion <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20code.20representation.20of.20expression.20from.20HIR.html#225927435">(Feb 10 2021 at 23:21)</a>:</h4>
<p>I'm trying to make a clippy lint that warns against the usage of e.g. <code>u16::from_str_radix(string, 10)</code> (the 10 is specific) and instead replace it with <code>string.parse()</code>.</p>
<p>To recommend the second option though, I need to have access to how the original expression that produced <code>string</code> looks like. I'm asking this here instead of at <a class="stream" data-stream-id="257328" href="/#narrow/stream/257328-clippy">#clippy</a> , because <code>LateLintPass</code> only gives me access to a <code>rustc_hir::Expr</code> and a <code>LateLintContext</code> when linting on expressions, which seem to come from the compiler and are not specific to clippy.</p>
<p>Is there any way that I can do what I want?</p>



<a name="225941378"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20code%20representation%20of%20expression%20from%20HIR/near/225941378" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20code.20representation.20of.20expression.20from.20HIR.html#225941378">(Feb 11 2021 at 02:24)</a>:</h4>
<p>Clippy deals with the compiler a lot so it's still not a bad idea to ask there</p>



<a name="225941396"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20code%20representation%20of%20expression%20from%20HIR/near/225941396" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20code.20representation.20of.20expression.20from.20HIR.html#225941396">(Feb 11 2021 at 02:24)</a>:</h4>
<p>I'm confused what you want to do - what do you mean by "what the expression looks like"?</p>



<a name="225958866"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20code%20representation%20of%20expression%20from%20HIR/near/225958866" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boolean_coercion <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20code.20representation.20of.20expression.20from.20HIR.html#225958866">(Feb 11 2021 at 08:11)</a>:</h4>
<p>I meant, for example, getting a string from some function invocation (Call or MethodCall) instead of a variable (or QPath, if I understand that part of HIR correctly) in the first argument of the function, and then using what the user wrote to suggest an alternative.</p>
<p>Those aren't the only two cases of course, and I'm sure there's a better way than stringifying each type of Expr manually <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="225958875"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20code%20representation%20of%20expression%20from%20HIR/near/225958875" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boolean_coercion <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20code.20representation.20of.20expression.20from.20HIR.html#225958875">(Feb 11 2021 at 08:11)</a>:</h4>
<p>I'll ask on <a class="stream" data-stream-id="257328" href="/#narrow/stream/257328-clippy">#clippy</a> too though</p>



<a name="226000958"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20code%20representation%20of%20expression%20from%20HIR/near/226000958" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20code.20representation.20of.20expression.20from.20HIR.html#226000958">(Feb 11 2021 at 14:48)</a>:</h4>
<p>I would recommend against using the original code for suggestions, it means the diagnostics are dependent on formatting which makes them more fragile</p>



<a name="226001198"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20code%20representation%20of%20expression%20from%20HIR/near/226001198" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20code.20representation.20of.20expression.20from.20HIR.html#226001198">(Feb 11 2021 at 14:50)</a>:</h4>
<p>you could do something like <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/source_map/struct.SourceMap.html#method.span_to_snippet"><code>span_to_snippet</code></a> on <code>string</code> and then show your suggestion from there</p>



<a name="226027572"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20code%20representation%20of%20expression%20from%20HIR/near/226027572" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boolean_coercion <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20code.20representation.20of.20expression.20from.20HIR.html#226027572">(Feb 11 2021 at 17:30)</a>:</h4>
<p>Someone on <a class="stream" data-stream-id="257328" href="/#narrow/stream/257328-clippy">#clippy</a> already helped me, but thanks :D</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>